﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>GetKeyState - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The GetKeyState function checks if a keyboard key or mouse/joystick button is down or up. Also retrieves joystick status." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>GetKeyState</h1>

<p>检查键盘按键或鼠标/操纵杆按键是否按下或放开. 也可以获取操纵杆的状态.</p>

<pre class="Syntax">IsDown := <span class="func">GetKeyState</span>(KeyName <span class="optional">, Mode</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>KeyName</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>This can be just about any single character from the keyboard or one of the key names from the <a href="../KeyList.htm">key list</a>, such as a mouse/joystick button. Examples: B, 5, LWin, RControl, Alt, Enter, Escape, LButton, MButton, Joy1.</p>
      <p>Alternatively, an explicit virtual key code such as vkFF may be specified. This is useful in the rare case where a key has no name. The code of such a key can be determined by following the steps at the bottom of the <a href="../KeyList.htm#SpecialKeys">key list page</a>. Note that this code must be in hexadecimal.</p>
      <p><b>已知限制:</b> This function cannot differentiate between two keys which share the same virtual key code, such as Left and NumpadLeft.</p>
  </dd>

  <dt>Mode</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>This parameter is ignored when retrieving joystick status.</p>
      <p>If omitted, the mode will default to that which retrieves the logical state of the key. This is the state that the OS and the active window believe the key to be in, but is not necessarily the same as the physical state.</p>
	    <p>Alternatively, one of these letters may be specified:</p>
      <p><strong>P</strong>: Retrieve the physical state (i.e. whether the user is physically holding it down). The physical state of a key or mouse button will usually be the same as the logical state unless the keyboard and/or mouse hooks are installed, in which case it will accurately reflect whether or not the user is physically holding down the key or button (as long as it was pressed down while the script was running). You can determine if your script is using the  hooks via the <a href="KeyHistory.htm">KeyHistory</a> function or menu item. You can force the hooks to be installed by adding the <a href="_InstallKeybdHook.htm">#InstallKeybdHook</a> and/or <a href="_InstallMouseHook.htm">#InstallMouseHook</a> directives to the script.</p>
      <p><strong>T</strong>: Retrieve the toggle state. For keys other than <kbd>CapsLock</kbd>, <kbd>NumLock</kbd> and <kbd>ScrollLock</kbd>, the toggle state is generally 0 when the script starts and is not synchronized between processes.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a>, <a href="../Concepts.htm#numbers">浮点数</a>或<a href="../Concepts.htm#numbers">整数</a></p>
<p>For keyboard keys and mouse buttons, this function returns 1 (true) if the key is down (or toggled on) or 0 (false) if it is up (or toggled off).</p>
<p>When <em>KeyName</em> is a joystick axis such as JoyX, this function returns a <a href="../Concepts.htm#numbers">floating-point number</a> between 0 and 100 to indicate the joystick's position as a percentage of that axis's range of motion. This <a href="../scripts/index.htm#JoystickTest">test script</a> can be used to analyze your joystick(s).</p>
<p>When <em>KeyName</em> is JoyPOV, this function returns an <a href="../Concepts.htm#numbers">integer</a> between 0 and 35900. The following approximate POV values are used by many joysticks:</p>
<ul>
    <li>-1: no angle to report</li>
    <li>0: forward POV</li>
    <li>9000 (i.e. 90 degrees): right POV</li>
    <li>27000 (i.e. 270 degrees): left POV</li>
    <li>18000 (i.e. 180 degrees): backward POV</li>
</ul>

<h2 id="Remarks">备注</h2>
<p>为了等待按键或鼠标/操纵杆按钮进入新的状态, 通常使用 <a href="KeyWait.htm">KeyWait</a> 代替 GetKeyState 的循环会更简单.</p>
<p>Systems with unusual keyboard drivers might be slow to update the state of their keys, especially the toggle-state of keys like <kbd>CapsLock</kbd>. A script that checks the state of such a key immediately after it changed may use <a href="Sleep.htm">Sleep</a> beforehand to give the system time to update the key state.</p>
<p>For examples of using GetKeyState with a joystick, 请参阅 the <a href="../misc/RemapJoystick.htm">joystick remapping page</a> and the <a href="../scripts/index.htm#JoystickMouse">Joystick-To-Mouse script</a>.</p>
<h2 id="Related">相关</h2>
<p><a href="GetKeyVK.htm">GetKeyVK</a>, <a href="GetKeySC.htm">GetKeySC</a>, <a href="GetKeyName.htm">GetKeyName</a>, <a href="KeyWait.htm">KeyWait</a>, <a href="../KeyList.htm">Key List</a>, <a href="../misc/RemapJoystick.htm">Joystick remapping</a>, <a href="KeyHistory.htm">KeyHistory</a>, <a href="_InstallKeybdHook.htm">#InstallKeybdHook</a>, <a href="_InstallMouseHook.htm">#InstallMouseHook</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>state := GetKeyState("RButton")  <em>; Right mouse button.</em>
state := GetKeyState("Joy2")  <em>; The second button of the first joystick.</em>

if GetKeyState("Shift")
    MsgBox "At least one Shift key is down."
else
    MsgBox "Neither Shift key is down."

state := GetKeyState(&quot;CapsLock&quot;, &quot;T&quot;)  <em>; True if CapsLock is ON, false otherwise.</em></pre>
</div>

<div class="ex" id="ExRemap">
<p><a href="#ExRemap">#2</a>: 重映射. (此示例仅用于说明, 因为<a href="../misc/Remap.htm">内置的重新映射功能</a>更容易使用.) 在下面的热键中, 按下 NumpadAdd 时, 鼠标按钮被按住, 这有效地将 NumpadAdd 变成了一个鼠标按键. 该方法还可以用于重复操作, 而用户正在按住一个键或按键:</p>
<pre>*NumpadAdd::
{
    MouseClick "left",,, 1, 0, "D"  <em>; Hold down the left mouse button.</em>
    Loop
    {
        Sleep 10
            if !GetKeyState("NumpadAdd", "P")  <em>; The key has been released, so break out of the loop.</em>
                break
            <em>; ... insert here any other actions you want repeated.</em>
    }
    MouseClick "left",,, 1, 0, "U"  <em>; Release the mouse button.</em>
}</pre>
</div>

<div class="ex" id="ExJoystick">
<p><a href="#ExJoystick">#3</a>: 使操纵杆按钮的行为取决于操纵杆轴的位置:</p>
<pre>joy2:: 
{
    JoyX := GetKeyState("JoyX")
    if (JoyX &gt; 75)
        MsgBox "Action #1 (button pressed while joystick was pushed to the right)."
    else if (JoyX &lt; 25)
        MsgBox "Action #2 (button pressed while joystick was pushed to the left)."
    else
        MsgBox "Action #3 (button pressed while joystick was centered horizontally)."
}</pre>
</div>

<h3 id="ex-other">其他示例</h3>
<p>请参阅<a href="../misc/RemapJoystick.htm">操纵杆重映射页面</a>和<a href="../scripts/index.htm#JoystickMouse">操纵杆到鼠标的映射脚本</a>的其他示例..</p>

</body>
</html>